﻿<%@ page language="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <%@ include file="../common/header.jsp" %>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <%@ include file="../common/top.jsp" %>

    <div class="row">
        <div class="col-sm-3">
            <c:set var="menu" value="employee"/>
            <%@ include file="../common/menu.jsp" %>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工管理</h1>
                </div>
            </div>
            <form class="form-inline" id="searchForm" action="/employee/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword"
                           value="${qo.keyword}" placeholder="请输入姓名/邮箱">
                </div>
                <div class="form-group">
                    <label for="dept">部门:</label>
                    <select class="form-control" id="dept" name="dept_id">
                        <option value="-1">全部部门</option>
                        <c:forEach var="d" items="${depts}">
                            <option value="${d.id}">${d.name}</option>
                        </c:forEach>
                    </select>
                  	<script>
                        $("#dept").val(${qo.dept_id});
                    </script>
                </div>
                <button type="submit" class="btn btn-primary">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="/employee/input.do" class="btn btn-success">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
                <a role="button" class="btn btn-danger batchDelete">
                    <span class="glyphicon glyphicon-trash"></span> 批量删除
                </a>
                <a href="/employee/exportExcel.do" target="_blank" class="btn btn-warning">
                    <span class="glyphicon glyphicon-export"></span> 导出
                </a>
                <a role="button" class="btn btn-warning importBtn">
                    <span class="glyphicon glyphicon-import"></span> 导入
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th><input type="checkbox" id="all"></th>
                    <th>编号</th>
                    <th>名称</th>
                    <th>email</th>
                    <th>年龄</th>
                    <th>部门</th>
                    <th>操作</th>
                </tr>
                 <c:forEach var="entity" items="${pageInfo.list}">
                     <tr>
                         <td><input type="checkbox" class="ck" data-eid="${entity.id}"></td>
                         <td>${entity.id}</td>
                         <td>${entity.name}</td>
                         <td>${entity.email}</td>
                         <td>${entity.age}</td>
                         <td>${entity.dept.name}</td>
                         <td>
                             <a href="/employee/input.do?id=${entity.id}" class="btn btn-info btn-xs">
                                 <span class="glyphicon glyphicon-pencil"></span> 编辑
                             </a>
                                 <a role="button" data-url="/employee/delete.do?id=${entity.id}"
                                    class="btn btn-danger btn-xs deleteBtn">
                                     <span class="glyphicon glyphicon-trash"></span> 删除
                                 </a>
                         </td>
                     </tr>
                 </c:forEach>
            </table>

            <%@ include file="../common/page.jsp" %>
        </div>
    </div>
</div>

<!-- 导入员工 -->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">导入员工</h4>
            </div>
            <div class="modal-body">
                <!--填充编辑界面-->
                <form id="editForm" class="form-horizontal" action="/employee/importExcel.do" enctype="multipart/form-data" method="post">
                    <input type="hidden" name="id"/>
                    <div class="form-group" >
                        <label for="name" class="col-lg-4 control-label">上传文件：</label>
                        <div class="col-lg-6">
                            <input type="file" name="xls" accept="application/vnd.ms-excel" class="form-control"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-lg-4 control-label">参考模板：</label>
                        <div class="col-lg-6">
                            <a href="/template/employee.xls" class="btn btn-success btn-block">
                                <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        //给选择所有的复选框添加点击事件
        $("#all").click(function () {
            $(".ck").prop("checked", this.checked);
        });

        //当所有的明细勾上时,选择所有的复选框也勾上
        $(".ck").click(function () {
            //选中的数量和所有的数量一致,说明全勾上了
            if ($(".ck").size() == $(".ck:checked").size()) {
                $("#all").prop("checked", true);
            } else {
                $("#all").prop("checked", false);
            }
        });
        
        //批量删除
        $(".batchDelete").click(function () {
            //判断有数据被选中
            if (!$(".ck:checked").size()) {
                //提示必须要有数据被选中
                $.messager.alert("温馨提示", "至少要选中一个数据");
                return;
            }
            //拿到被勾选的数据的id
            var ids = $.map($(".ck:checked"), function (ele) {
                return $(ele).data("eid");
            });

            //点击确认后才发送请求
            $.messager.confirm("温馨提示", "亲,你确定要批量删除吗", function () {
                //发送ajax请求删除数据
                $.post("/employee/batchDelete.do", {ids: ids}, function (data) {
                    doSuccess();
                });
            });
        });
        
        //点击导入按钮弹出模态框
        $(".importBtn").click(function () {
            $("#importModal").modal("show");
        });

        $(".submitBtn").click(function () {
            $("#editForm").submit();
        });
    });
</script>
</body>
</html>